// utils/helpers.js - 工具函数
import { TASK_STATUS } from './constants'

/**
 * 格式化日期
 */
export function formatDate(date, format = 'YYYY-MM-DD') {
    if (!date) return ''

    const d = new Date(date)
    const year = d.getFullYear()
    const month = String(d.getMonth() + 1).padStart(2, '0')
    const day = String(d.getDate()).padStart(2, '0')
    const hour = String(d.getHours()).padStart(2, '0')
    const minute = String(d.getMinutes()).padStart(2, '0')
    const second = String(d.getSeconds()).padStart(2, '0')

    return format
        .replace('YYYY', year)
        .replace('MM', month)
        .replace('DD', day)
        .replace('HH', hour)
        .replace('mm', minute)
        .replace('ss', second)
}

/**
 * 格式化时间为相对时间
 */
export function formatRelativeTime(date) {
    if (!date) return ''

    const now = new Date()
    const target = new Date(date)
    const diff = now - target

    const minute = 60 * 1000
    const hour = 60 * minute
    const day = 24 * hour
    const week = 7 * day
    const month = 30 * day
    const year = 365 * day

    if (diff < minute) {
        return '刚刚'
    } else if (diff < hour) {
        return `${Math.floor(diff / minute)}分钟前`
    } else if (diff < day) {
        return `${Math.floor(diff / hour)}小时前`
    } else if (diff < week) {
        return `${Math.floor(diff / day)}天前`
    } else if (diff < month) {
        return `${Math.floor(diff / week)}周前`
    } else if (diff < year) {
        return `${Math.floor(diff / month)}个月前`
    } else {
        return `${Math.floor(diff / year)}年前`
    }
}

/**
 * 获取任务状态信息
 */
export function getTaskStatusInfo(status) {
    return TASK_STATUS[status] || { value: status, label: status, type: 'info' }
}

/**
 * 验证表单数据
 */
export function validateSearchForm(form) {
    const errors = []

    if (!form.departure) {
        errors.push('请选择出发地')
    }

    if (!form.destination) {
        errors.push('请选择目的地')
    }

    if (form.departure === form.destination) {
        errors.push('出发地和目的地不能相同')
    }

    if (!form.departDate) {
        errors.push('请选择出发日期')
    }

    if (form.tripType === 'roundtrip' && !form.returnDate) {
        errors.push('往返行程请选择返程日期')
    }

    if (form.tripType === 'roundtrip' && form.returnDate && form.departDate) {
        const departDate = new Date(form.departDate)
        const returnDate = new Date(form.returnDate)
        if (returnDate <= departDate) {
            errors.push('返程日期必须晚于出发日期')
        }
    }

    const today = new Date()
    today.setHours(0, 0, 0, 0)
    if (form.departDate && new Date(form.departDate) < today) {
        errors.push('出发日期不能早于今天')
    }

    if (form.adults < 1 || form.adults > 9) {
        errors.push('成人数量必须在1-9之间')
    }

    if (form.children < 0 || form.children > 9) {
        errors.push('儿童数量必须在0-9之间')
    }

    if (form.infants < 0 || form.infants > 9) {
        errors.push('婴儿数量必须在0-9之间')
    }

    return errors
}

/**
 * 解析Cron表达式描述
 */
export function parseCronExpression(cron) {
    if (!cron) return '无效表达式'

    const parts = cron.trim().split(/\s+/)
    if (parts.length !== 6) return '格式错误'

    const [second, minute, hour, day, month, week] = parts

    let description = ''

    // 解析时间
    if (hour === '*') {
        if (minute === '*') {
            description = '每分钟'
        } else if (minute.includes('/')) {
            const interval = minute.split('/')[1]
            description = `每${interval}分钟`
        } else {
            description = `每小时${minute}分`
        }
    } else if (hour.includes('/')) {
        const interval = hour.split('/')[1]
        description = `每${interval}小时`
    } else {
        const h = parseInt(hour)
        const m = parseInt(minute) || 0
        description = `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}`
    }

    // 解析日期
    if (week !== '?' && week !== '*') {
        if (week === '1-5') {
            description += ' (工作日)'
        } else if (week === '6,0') {
            description += ' (周末)'
        } else {
            const weekNames = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
            const days = week.split(',').map(d => weekNames[parseInt(d)] || `周${d}`).join('、')
            description += ` (${days})`
        }
    } else if (day !== '*' && day !== '?') {
        if (day.includes('/')) {
            const interval = day.split('/')[1]
            description += ` 每${interval}天`
        } else {
            description += ` 每月${day}日`
        }
    } else {
        description += ' 每天'
    }

    return description
}

/**
 * 深拷贝对象
 */
export function deepClone(obj) {
    if (obj === null || typeof obj !== 'object') return obj
    if (obj instanceof Date) return new Date(obj.getTime())
    if (obj instanceof Array) return obj.map(item => deepClone(item))
    if (typeof obj === 'object') {
        const cloned = {}
        Object.keys(obj).forEach(key => {
            cloned[key] = deepClone(obj[key])
        })
        return cloned
    }
}